﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

using Net.SyntaxC4.WindowsAzure.EmailQueue.BLL;

namespace Net.SyntaxC4.WindowsAzure.EmailQueue.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void SendEmail_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                //TODO: Create Cloud Queue
            CloudQueueClient cloudQueueClient =
                CloudStorageAccount.DevelopmentStorageAccount.CreateCloudQueueClient();

                CloudQueue cloudQueue =
                    cloudQueueClient.GetQueueReference(Globals.QueueStorageName);

                cloudQueue.CreateIfNotExist();
                // Add Information to Queue for Storage and Sending
                string message = string.Format("{0}||{1}||{2}||{3}||{4}", 
                                                _EmailFrom.SelectedItem.Text,
                                                _EmailTo.Text,
                                                _EmailSubject.Text,
                                                _EmailMessage.Text,
                                                UploadEmailAttachment());

                //TODO: Create Queue Message
                cloudQueue.AddMessage(new CloudQueueMessage(message));
            }
        }

        private string UploadEmailAttachment()
        {
            // Make sure there is a File to Upload
            if (_EmailAttachmentUpload.HasFile &&
                _EmailAttachmentUpload.PostedFile.ContentLength > 0)
            {
                //TODO: Create Cloud Blob
                // Get the Storage Account
                CloudStorageAccount storageAccount = 
                    CloudStorageAccount.DevelopmentStorageAccount;

                // Blob Storage Reference
                CloudBlobClient storageClient = 
                    storageAccount.CreateCloudBlobClient();

                // Blob Container
                CloudBlobContainer container = 
                    storageClient.GetContainerReference(Globals.BlobStorageName);

                string fileName = string.Format("{0}_{1}", DateTime.Now.Ticks,
                                                System.IO.Path.GetFileName(
                                                _EmailAttachmentUpload.PostedFile.FileName));
                
                //TODO: Push Image into Blob 
                // Actual Blob, If it Exists or Not
                CloudBlob blob = container.GetBlobReference(fileName);

                blob.Properties.ContentType = _EmailAttachmentUpload.PostedFile.ContentType;
                blob.UploadFromStream(_EmailAttachmentUpload.PostedFile.InputStream);
                

                return fileName;
            }
            return string.Empty;
        }
    }
}
